package andy.demo.slip.viewpager;


import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Parcelable;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import andy.demo.R;

@SuppressLint({ "ParserError", "WrongViewCast", "ResourceAsColor" })
public class ViewPagerTest extends Activity implements OnClickListener {
	private static final String TAG = ViewPagerTest.class.getSimpleName();
	// VIewPaer控件
	private ViewPager viewPager;
	// VIewPager的数据源
	private MyPagerAdapter myAdapter;
	// 为VIewPager保存VIew界面的集合
	private List<View> vessel = new ArrayList<View>();
	// 代表第一个和第2个界面
	private View view1, view2;
	// 动画图片
	private ImageView cursor;
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
	// 控制右侧帮助页卡显示的开关
	private boolean result = true;
	// 包含右边导航栏的层
	private LinearLayout lin2;
	// 包含ViewPage等页面的层
	private RelativeLayout lin1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slide_viewpager_main);
		InitImageView();
		init();
	}

	/**
	 * 初始化控件和绑定事件
	 */
	@SuppressLint("ResourceAsColor")
	public void init() {

		view1 = this.getLayoutInflater().inflate(R.layout.slide_viewpager_layout1, null);
		view2 = this.getLayoutInflater().inflate(R.layout.slide_viewpager_layout2, null);
		vessel.add(view1);
		vessel.add(view2);
		viewPager = (ViewPager) findViewById(R.id.viewpagerLayout);
		myAdapter = new MyPagerAdapter();
		viewPager.setAdapter(myAdapter);
		// 设置起始默认的值为0
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		cursor = (ImageView) findViewById(R.id.cursor);
		lin1 = (RelativeLayout) findViewById(R.id.lin1);
		lin2 = (LinearLayout) findViewById(R.id.lin2);
		// 为VIewPage控件设置触摸事件
		viewPager.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Log.d(TAG, "onTouch## ");
				// 如果当前当行卡为显示的，并且触摸了VP那么就关闭导航卡
				if (result == false) {
					System.out.println("监听Lin的触摸监听");
					viewPager.setBackgroundColor(getResources().getColor(
							R.color.darkgray));
					lin1.scrollBy(80, 0);
					lin1.scrollTo(0, 0);
					lin2.setVisibility(8);
					// lin2.setAnimation(AnimationUtils.loadAnimation(
					// Win8StyleActivity.this, R.anim.push_left_out));
					// 返回为true 不让VP滑动
					result = true;
					return true;
				}
				// 否则就滑动VP选项卡
				return false;
			}
		});
	}

	/*
	 * 初始化滑动条
	 */
	private void InitImageView() {

		cursor = (ImageView) findViewById(R.id.cursor);

		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
				.getWidth();// 获取图片宽度

		DisplayMetrics dm = new DisplayMetrics();

		getWindowManager().getDefaultDisplay().getMetrics(dm);

		int screenW = dm.widthPixels;// 获取分辨率宽度
		System.out.println("bmpw:" + bmpW + "screenW:" + screenW);
		offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		System.out.println("offset:" + offset);
		cursor.setImageMatrix(matrix);// 设置动画初始位置

	}

	// 适配器
	class MyPagerAdapter extends PagerAdapter {

		public void destroyItem(View arg0, int arg1, Object arg2) {
			Log.d(TAG, "destroyItem ## ");
			((ViewPager) arg0).removeView(vessel.get(arg1));
		}

		public void finishUpdate(View arg0) {
			Log.d(TAG, "finishUpdate## ");
		}

		public int getCount() {
			Log.d(TAG, "getCount##");
			return vessel.size();
		}

		public Object instantiateItem(View arg0, int arg1) {
			Log.d(TAG, "instantiateItem ## ------------------arg1=" + arg1);
			((ViewPager) arg0).addView(vessel.get(arg1), 0);
			return vessel.get(arg1);
		}

		public boolean isViewFromObject(View arg0, Object arg1) {
			Log.d(TAG, "isViewFromObject## ==========");
			return arg0 == (arg1);
		}

		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			Log.d(TAG, "restoreState##rrrrrrrrrrrrrrrrrrrr");
		}

		public Parcelable saveState() {
			Log.d(TAG, "saveState##sssssssssssssssssssss");
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
			Log.d(TAG, "startUpdate## start start start");
		}
	}

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量

		// int two = one * 2;// 页卡1 -> 页卡3 偏移量
		@Override
		public void onPageSelected(int arg0) {
			System.out.println("onPageSelected## one = "+ one +" arg0:" + arg0 + "currenIndex:"
					+ currIndex);
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					 animation = new TranslateAnimation(one, 0, 0, 0);
				}/*
				 * else if (currIndex == 2) { animation = new
				 * TranslateAnimation(two, 0, 0, 0); }
				 */
				break;
			case 1:
				if (currIndex == 0) {
					 animation = new TranslateAnimation(offset, one, 0, 0);
				} /*
				 * else if (currIndex == 2) { animation = new
				 * TranslateAnimation(two, one, 0, 0); }
				 */
				break;
			/*
			 * case 2: if (currIndex == 0) { animation = new
			 * TranslateAnimation(offset, two, 0, 0); } else if (currIndex == 1)
			 * { animation = new TranslateAnimation(one, two, 0, 0); } break;
			 */
			}

			currIndex = arg0;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			cursor.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			Log.d(TAG, "onPageScrolled##");
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			Log.d(TAG, "onPageScrollStateChanged## arg0="+ arg0);
		}
	}

	/**
	 * | 点击事件
	 */
	@SuppressLint({ "ResourceAsColor", "ResourceAsColor" })
	public void onClick(View v) {
		switch (v.getId()) {
		// case R.id.layout2_btn_ok:
		// Toast.makeText(this, "第2个页面的BUtton", 50).show();
		// break;
		// case R.id.layout_btn_ok:
		// Toast.makeText(this, "第1个页面的BUtton", 50).show();
		// // EditText tv=(EditText)view1.findViewById(R.id.layout_txt_tv1);
		// // tv.setText("第"+count+"次");
		// count++;
		// break;

		}

	}
}
